package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.EnergyLevel;
import com.juyu.miliao.api.domain.IconOfficial;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.service.cache.IconOfficialCacheService;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.netease.NetEaseCloudUserService;
import com.juyu.miliao.common.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: 卜加超
 * @Date: 2019/7/11 09:15
 * @Description:
 */
@Service
@Slf4j
public class IconOfficialService {
    private final Logger logger = LoggerFactory.getLogger(IconOfficialService.class);
    @Resource
    private IconOfficialCacheService iconOfficialCacheService;
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private UserRedisDao userRedisDao;
    @Resource
    private EnergyLevelService energyLevelService;

    public List<IconOfficial> getIconList() {
        return iconOfficialCacheService.getIconList();
    }

    public void updateIcon(long id, String iconUrl, int iconType, int userIcon) {
         User user = userInfoDao.getUserInfoById(id);
         if(user.getIsHost()==2){
             EnergyLevel userEnergyLevel = energyLevelService.getUserEnergyLevel(user.getEnergySum());
             if(userEnergyLevel.getEnergyGrade()<6){
                 if(userIcon!=0){
                     userInfoDao.updateIcon(id,iconUrl,1,userIcon);
                     sendYunXinUpdateIcon(id,iconUrl);
                 }else {
                     throw  new ConsumeRuntimeException("您的等级不够，无法解锁自定义头像");
                 }
             }else {
                 userInfoDao.updateIcon(id,iconUrl,iconType,userIcon);
                 sendYunXinUpdateIcon(id,iconUrl);
                 if(iconType==2){
                     userRedisDao.userInfoCheckIcon(user.getUserId());
                 }

             }
         }else if(user.getIsHost()==1){
             userRedisDao.userInfoCheckIconForHost(id,iconUrl);
         }
    }

    /**
     * 将修改后的头像同步到云信
     * @param id
     * @param iconUrl
     */
    public void sendYunXinUpdateIcon(long id,String iconUrl){
        logger.info("用户修改操作:参数:userId={},icon={}", id, iconUrl);
        try {
            if ( iconUrl != null) {
                String createLogInfo = NetEaseCloudUserService.updateUserInfo(id, "",
                        iconUrl);
                if (!createLogInfo.substring(createLogInfo.indexOf("code") + 6, createLogInfo.indexOf("code") + 9)
                        .equals("200")) {
                    log.error("更新用户基本信息IM失败:系统错误信息:createLogInfo={},参数:userId={},nickName={},icon={}", createLogInfo, id
                            , "", iconUrl);
                }
            }
        } catch (Exception e) {
            log.error("更新用户基本信息IM错误:参数-" + id + "" + iconUrl, e);
        }
    }
}
